package com.ants.boot.wx.service.impl;

import com.ants.boot.wx.dao.WxArticleMapper;
import com.ants.boot.wx.domain.WxArticle;
import com.ants.boot.wx.dto.wxarticle.WxPageInfoDto;
import com.ants.boot.wx.service.WxArticleCommentService;
import com.ants.boot.wx.service.WxArticleService;
import com.ants.boot.wx.vo.wxarticle.DetailArticleVo;
import com.ants.boot.wx.vo.wxarticlecomment.WxArticleCommentVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 蚂蚁会花呗
 * @date 2022/6/7 10:08
 */
@Service
public class WxArticleServiceImpl extends ServiceImpl<WxArticleMapper, WxArticle> implements WxArticleService {

    @Autowired
    private WxArticleMapper wxArticleMapper;

    @Autowired
    private WxArticleCommentService wxArticleCommentService;


    @Override
    public List<WxArticle> wxPageInfo(WxPageInfoDto dto) {
        Page<WxArticle> page = new Page<>(dto.getPageNum(),dto.getPageSize());
        IPage<WxArticle> iPage = wxArticleMapper.wxPageInfo(page,dto);
        return iPage.getRecords();
    }

    @Override
    public DetailArticleVo getDetailArticle(Long id) {
        DetailArticleVo vo = wxArticleMapper.wxSelectOne(id);
        List<WxArticleCommentVo> list = wxArticleCommentService.listComment(id);
        if (!list.isEmpty()){
            List<WxArticleCommentVo> comments = new ArrayList<>();
            List<WxArticleCommentVo> replays = new ArrayList<>();
            list.forEach(wx ->{
                if (wx.getParentId()==0 && wx.getReplyId() == 0){
                    comments.add(wx);
                }else {
                    replays.add(wx);
                }
            });

            vo.getCommentList().addAll(comments);
            vo.getCommentList2().addAll(replays);
        }
        return vo;
    }
}
